﻿using System;
using System.Linq;
using System.Web.Mvc;
using KPIS.GERP.Models.ICM;

namespace KPIS.GERP.WEB.GIMS.Controllers
{
    // [AccessDeniedAuthorize]
    public class DeliveryNoteSelectController : Controller
    {
        //
        // GET: /DeliveryNoteSelect/

        public ActionResult Index()
        {
            {
                return View(new DeliveryNote());
            }

        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(DeliveryNote model,int PoId,int PoInstallId)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var service = new DeliveryNoteService.DeliveryNoteServiceClient();


                    // model = 1;// new UserService.UserServiceClient().GetEmployeeByUser(Class.GimsMembership.CurrentEmpId).EmpId;
                    var id = service.Insert(model, Class.GimsMembership.CurrentEmpId);
                    return RedirectToAction("Index", new { id });
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return View(model);
        }


        public ActionResult Edit(int id)
        {
            var service = new DeliveryNoteService.DeliveryNoteServiceClient();
            var item = service.SelectDeliveryNote(new Models.ICM.DeliveryNote { DnId = id, RecordStatus = "!D" }, null).FirstOrDefault();

            if (item == null)
            {
                return HttpNotFound();
            }
            return View(item);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(DeliveryNote model)
        {

            try
            {
                if (ModelState.IsValid)
                {

                    // var service = new DeliveryNoteService.DeliveryNoteServiceClient();

                    return RedirectToAction("Index");
                }
                return Edit(model.DnId);
            }
            catch
            {
                return Edit(model.DnId);
            } 
        }

    }
}
